<?php

namespace App\Http\Controllers\Admin\Sponsor;

use App\Exceptions\SponsorException;
use App\Services\Admin\Sponsor\SlaughterService;
use Exception;
use Illuminate\Http\JsonResponse;

class SlaughterController extends BaseSponsorController
{
    protected SlaughterService $slaughterService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->slaughterService = $this->loadService('Admin\Sponsor\SlaughterService');
    }

    /**
     * 宰杀申请列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->slaughterService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 宰杀申请详情
     *
     * @return JsonResponse
     * @throws SponsorException
     */
    public function detail(): JsonResponse
    {
        $detail = $this->slaughterService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 审批
     * @return JsonResponse
     * @throws SponsorException
     */
    public function examine(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'status' => 'required|integer|min:1',
            'remark' => 'max:128',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->slaughterService->examine($this->params);
        return outputJsonResult();
    }

    /**
     * 宰杀寄回
     * @return JsonResponse
     * @throws Exception
     */
    public function sendBack(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'expressCompanyCode' => 'required|string|min:1',
            'expressNo' => 'required|string|min:1',
            'remark' => 'max:128',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->slaughterService->sendBack($this->params);
        return outputJsonResult();
    }

    /**
     * 发放积分
     * @return JsonResponse
     * @throws SponsorException
     */
    public function grantIntegral(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'quantity' => 'required|integer|min:1',
            'remark' => 'max:128',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->slaughterService->grantIntegral($this->params);
        return outputJsonResult();
    }
}
